#pragma once

#include <QObject>
class QTcpServer;
class QNetworkSession;
class seven::TransferMessage;
class ServerConnection;
class FileRecvManager;

class ServerNetsystem : public QObject
{
	Q_OBJECT

public:
	ServerNetsystem(QObject *parent);
	~ServerNetsystem();

	void Start();	
	QString GetPort();

	private slots:
		void sessionOpened();
		void AcceptConnection();
		void HandleMsg(const seven::TransferMessage& msg); 
		void RequireFile(int fileId);
		void TransferFinish();

	
protected:
	enum ServerState
	{
		NONE,
		START,
		CONNECTED,
		PATH_TABLE_FINISHED,
		FILE_TABLE_FINISHED
	};

	void OnPATH_TABLE_FINISHED(const seven::TransferMessage& msg);
	void OnFILE_TABLE_FINISHED(const seven::TransferMessage& msg);
	void OnFILE_CONTENT_RSP(const seven::TransferMessage& msg);
	
protected:
	bool SendMsg(seven::TransferMessage& msg);
	bool SendMsg(seven::MessageType msg);

	void SetState(ServerState state) {serverState = state; }
	ServerState GetState() { return serverState; }
	void Reset();

private:
	QTcpServer *tcpServer;
	ServerConnection * serverConnection;
	ServerState serverState;
	QSharedPointer<FileRecvManager> fileRecvMgr;
};

